<?php
namespace App\Http\Controllers\Admin;
use App\Exceptions\ExceptionSms;
use App\Models\AdminMenu;
use App\Models\BaseModel;
use App\Requests\AdminMenu\StoreRequest;
use App\Services\Common\AdminService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;

/**
 * 权限管理
 * Class AdminMenuController
 * @package App\Http\Controllers\Admin
 */
class AdminMenuController extends CommonController
{
    // 主页
    public function index()
    {
        $cols = [[
           ['field' => 'name', 'title' => '名称'],
           ['field' => 'menu_no', 'title' => '编码'],
           ['field' => 'uri', 'title' => '路由'],
           ['field' => 'ftype', 'title' => '类型'],
           ['field' => 'btn_ftype', 'title' => '按钮'],
           ['field' => 'ficon', 'title' => '图标', 'templet' => '#list-icon'],
           ['field' => 'sort', 'title' => '排序'],
           ['field' => 'action', 'title' => '操作', 'templet' => '#list-bar'],
        ]];

        $permission_code = 'menu-query';

        $uri = '/admin/menu/list?code=' . $permission_code;

        $top_btns = [
            ['code' => 'menu-add']
        ];

        $row_btns = [
          ['code' => 'menu-edit'],
          ['code' => 'menu-delete'],
        ];

        return view('components.list_tree', compact('cols', 'permission_code', 'uri', 'top_btns', 'row_btns'));
    }
    // 查询
    public function dataList()
    {
        $list = AdminMenu::with(['children'])->get();
        /** @var AdminMenu $item */
        foreach ($list as $item) {
            $item->isParent = collect($item->children)->count() ? true : false;
            $item->setAppends(['ftype', 'btn_ftype']);
            if ($item->icon) {
                $item->ficon = '<i class="layui-icon '.$item->icon.'"></i>';
            } else {
                $item->ficon = '';
            }
        }
        $data = array_to_tree(json_decode(json_encode($list), true), 'id', 'pid', 'children');

        return ExceptionSms::homeList($data, 0);
    }
    // 表单
    public function editAdd()
    {
        $id = $this->input('id');

        $data = $id ? AdminMenu::getOneById($id) : null;

        $save_forms = AdminMenu::getForms(['pid', 'name', 'menu_no', 'type', 'btn_type', 'is_export', 'icon', 'class_name', 'uri', 'sort']);
        if ($id) {
            $path = $data->path;
            unset($path[count($path)-1]);
            $save_forms['pid']['value'] = $path;
        }
        $permission_code = $id ? 'menu-edit-save' : 'menu-add-save';

        return view('components.save_form', compact('data', 'save_forms', 'permission_code'));
    }
    // 存储
    public function dataSave(StoreRequest $request)
    {
        $id = $request->get('id') ?? "";
        if ($id) $data = AdminMenu::getOneById($id);
        else $data = new AdminMenu();
        $pid = $request->pid ?? 0;
        $pid = explode(',', $pid);
        $all = $pid;
        $pid = end($pid);
        if ($pid == 0){
            $level = 1;
        } else {
            $level = count($all);
        }
        $data->menu_no      = $request->get("menu_no") ?? "";
        $data->name         = $request->get("name") ?? "";
        $data->class_name   = $request->get("class_name") ?? "";
        $data->pid          = $pid;
        $data->level        = $level;
        $data->type         = $request->get("type") ?? 0;
        $data->is_win       = $request->get("is_win") ?? 0;
        $data->icon         = $request->get("icon") ?? "";
        $data->uri          = $request->get("uri") ?? "";
//        $data->template_uri = $request->get("template_uri") ?? "";
        $data->sort         = $request->get("sort") ?? 0;
        $data->btn_type     = $request->get("btn_type") ?? 0;
        $data->is_export    = $request->get("is_export") ?? 0;

        if ($data->pid) {
            $parent      = AdminMenu::find($data->pid);
            $data->level = $parent ? $parent->level + 1 : 1;
        }
        if ($data->level == 2 || $data->uri) {
            $data->is_win = 1;
        }

        $data->save();

        AdminService::setPermissionCache(admin()->admin());

        // 更新菜单文件
        $all = AdminMenu::get();
        $all = json_decode(json_encode($all), true);
        $all = list_go_tree($all, 'id', 'pid', 'children', 0);
        $text = json_encode($all, JSON_UNESCAPED_UNICODE);
        $filename = Carbon::now()->format('ymd').'-menu.json';
        Storage::disk('local')->put($filename, $text);

        return ExceptionSms::returnSuccess();
    }
    // 删除
}
